<template>
  <div class="divBox relative">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
		
        <div class="container mt-1">
          <el-form inline size="small">
			<div class="flex items-center search_form">
				<!-- <div>
					商品名称：
					<el-input
					v-model="searchForm.title"
					placeholder="请输入商品名称"
					style="width: 200px; margin-right: 10px;"
					size="small"
					></el-input>
				</div>
				<div>
					所属人：
					<el-select v-model="searchForm.owner_id" style="width: 200px; margin-right: 10px;" size="small" placeholder="请选择所属人">
					  <el-option
					    v-for="item in shopUser"
					    :key="item.id"
					    :label="item.name"
					    :value="item.id"
					  />
					</el-select>
				</div> -->
				 <div>
					<el-button type="primary" size="mini" @click="addBtn">新增商品</el-button>
					<!-- <el-button size="mini" @click="reset">重置</el-button> -->
				 </div>
			</div>
			
           
			
           
          </el-form>
        </div>
      </div>
      <el-table
        :data="list"
        style="width: 100%"
        size="mini"
        height="600px"
        :highlight-current-row="true"
        :header-cell-style=" {fontWeight:'bold'}"
      >
        <el-table-column
          prop="id"
          label="ID"
          min-width="50"
        />
        <el-table-column label="商品名称"
	  					 align="center"
                         prop="goods_name"
                         min-width="200"
                         :show-overflow-tooltip="true">
		
        </el-table-column>
		<el-table-column  label="商品图片"   align="center">
			      <template slot-scope="scope">
			         <div>
						<el-image style="width: 100px; height: 50px" 
						:preview-src-list="[$baseUrl+'/storage/'+scope.row.image]"
						:src="$baseUrl+'/storage/'+scope.row.image"></el-image>
			 		</div>
			      </template>
			  </el-table-column>
		<el-table-column label="商品价格"
			  			 align="center"
		                 prop="price"
		                 min-width="100"
		                 :show-overflow-tooltip="true">
		</el-table-column>
		<el-table-column label="商品原始价格"
			  			 align="center"	
		                 prop="original_price"
		                 min-width="100"
		                 :show-overflow-tooltip="true">
		</el-table-column>
		<el-table-column label="购买赠送积分"
							 align="center"
		                 prop="reward_points"
		                 min-width="100"
		                 :show-overflow-tooltip="true">
		</el-table-column>
		<el-table-column label="用户兑换需要"
							 align="center"
		                 prop="need_points"
		                 min-width="200"
		                 :show-overflow-tooltip="true">
			            <template slot-scope="scope">
			                <div class="flex items-center" style="justify-content: center;">
			                    积分：{{ scope.row.need_points || 0 }}
			                    金额：{{ scope.row.need_price || 0 }}
			                </div>
			            </template>
			        </el-table-column>
		<el-table-column label="合作商兑换需要"
		                 prop="hzs_need_points"
		                 min-width="100"
							 align="center"
		                 :show-overflow-tooltip="true">
			            <template slot-scope="scope">
			                <div class="flex" style="justify-content: center;">
			                    积分：{{ scope.row.hzs_need_points || 0 }}
			                </div>
			            </template>
			        </el-table-column>			
        <el-table-column
          label="添加时间"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.add_time }}</span>
          </template>
        </el-table-column>
		<el-table-column
		  label="更新时间"
		  min-width="120"
		  align="center"
		>
		  <template slot-scope="scope">
		    <span>{{ scope.row.update_time }}</span>
		  </template>
		</el-table-column>
		
        <el-table-column label="操作" min-width="130" fixed="right">
          <template slot-scope="scope">
			<el-button type="text" @click="editRow(scope.row)">编辑</el-button>
			<el-button type="text" @click="delRow(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
	  <div class="page">
	              <el-pagination
	                      background
	                      @size-change="pageChange"
	                      @current-change="handleCurrentChange"
	                      :current-page="page"
	                      :page-size="pageSize"
	                      layout="total, sizes, prev, pager, next, jumper"
	                      :total="page_totle">
	              </el-pagination>
	          </div>
    </el-card>
	
	<!-- 新增角色 -->
	<el-dialog
	  :title="title"
	  :visible.sync="formShow"
	  :close-on-click-modal="false"
	  width="1000px">
	  <div style="height: 600px;overflow-y: scroll;">
			<el-form ref="form" :model="form" label-width="120px" >
		  <el-form-item label="商品名称">
		    <el-input v-model="form.goods_name" size="small" ></el-input>
		  </el-form-item>
		  <el-form-item label="商品图片">
		    <uploadImg ref="uploadImg" @getImgUrl="getImgUrl" />
		  </el-form-item>
		  <el-form-item label="商品规格">
		    <el-input v-model="form.spec" size="small" ></el-input>
		  </el-form-item>
		  <el-form-item label="商品价格">
		  	  <el-input v-model="form.price" size="small" ></el-input>
		   </el-form-item>
			<el-form-item label="商品原价">
		  	    <el-input v-model="form.original_price" size="small" ></el-input>
		  	</el-form-item>
			<el-form-item label="购买赠送积分">
		  	    <el-input v-model="form.reward_points" size="small" ></el-input>
		  	</el-form-item>
			<el-form-item label="用户兑换需要">
				<div class="flex">
					积分：
		  	    	<el-input type="number" style="width: 150px;margin-right: 10px;" v-model="form.need_points" size="small" ></el-input>
					金额：
					<el-input type="number" style="width: 150px;" v-model="form.need_price" size="small" ></el-input>
				</div>
		  	</el-form-item>
			<el-form-item label="合作商兑换需要">
		  	      积分：
		  	      <el-input type="number" style="width: 150px;margin-right: 10px;" v-model="form.hzs_need_points" size="small" ></el-input>
		  	</el-form-item>
		  <el-form-item label="商品详情">
		  	  <edit v-if="formShow" ref="myEdit" />
		  </el-form-item>
	  </el-form>
	  
	  </div>
	  <span slot="footer" class="dialog-footer">
	    <el-button size="mini" @click="formShow = false">取 消</el-button>
	    <el-button size="mini" type="primary" @click="confirm">确 定</el-button>
	  </span>
	</el-dialog>
  </div>
</template>
<script>
	import  edit  from "@/components/wangEditor.vue"
	import  uploadImg  from "@/components/uplodeImg.vue"
  export default {
	components:{
		edit,
		uploadImg
	},
    data() {
      return {
        title:'新增',
		isShow:false,
		form:{
			id:null,
			content:"",
			goods_name:"",
			price:"",
			original_price:"",
			reward_points:"",
			need_points:"",
			spec:"",
			image:"",
			need_price:"",
			hzs_need_points:"",
		},
		list:[],
		page:1,
		pageSize:10,
      }
    },
    mounted() {
      this.lodeList()
    },
    methods: {
		getImgUrl(url){
			this.form.image = url
		},
		reset(){
			this.searchForm = {
				title:"",
				owner_id:""
			}
			this.mySearch()
		},
		lodeStaffList(){
				 
			this.$api.post('/sadmin/getEmpList',{}).then(res=>{
						  let data = res.data
						  this.shopUser = data.result
			})
		},
		addBtn(){
			this.formShow = true
			this.form = {
				id:null,
				content:"",
				goods_name:"",
				price:"",
				original_price:"",
				reward_points:"",
				need_points:"",
				spec:"",
				image:"",
				need_price:"",
				hzs_need_points:"",
			}
			 this.$refs.uploadImg.url = ''
			 this.$refs.myEdit.html = ''
			this.title = '新增商品'
		},
		editRow(row,type){
			
			this.type = type
			this.addTitle = '编辑'
			for(let i in this.form){
				this.form[i] = row[i]
			}
			setTimeout(()=>{
			    this.$refs.uploadImg.url = row.image
				this.$refs.myEdit.html = row.content
			},100)
			this.formShow = true
		
		},
		
		confirm(){
			let params = {
				id:this.form.id,
				content:this.$refs.myEdit.html,
				goods_name:this.form.goods_name,
				price:this.form.price,
				original_price:this.form.original_price,
				reward_points:this.form.reward_points,
				need_points:this.form.need_points,
				spec:this.form.spec,
				image:this.form.image,
				need_price:this.form.need_price,
				hzs_need_points:this.form.hzs_need_points,
			}
		    this.$api.post('/admin/addEditGoods',params).then(res=>{
		    	  if(res.code==200){
					  this.formShow = false
		    	  		this.lodeList()
						this.$message({
								type: 'success',
								message: res.msg
							})
		    	  }else{
						this.$message({
								type: 'error',
								message: res.msg
							})
				  }
		    })
		},
		delRow(id){
			this.$confirm('确定删除', '提示', {
					  confirmButtonText: '确定',
					  cancelButtonText: '取消',
					  type: 'warning'
					}).then(() => {
						let params = {
							dd:{id}
						}
					  this.$api.post('/admin/delGoods',params).then(res=>{
					  	  if(res.code==200){
					  	  	this.lodeList()
					  	  }
					  })
			  })
	   },
      lodeList(){
		  let map = []
		  let params = {
		          	page:this.page,
		          	pageSize:this.pageSize,
					map:map
		    }
		 
		  this.$api.post('/admin/getGoodsList',params).then(res=>{
			  let data = res.data
			  this.page_totle = data.count
			  this.list = data.result
		  })
	  }
      
    }
  }
</script>
